package com.gxinyu.needle.login

import android.os.Bundle
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.gxinyu.needle.constans.LOGIN_NAME
import com.gxinyu.needle.constans.LOGIN_PASSWORD
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch

/**
 * @author gxinyu
 * @date  2022/5/8
 * @desc
 */
class LoginViewModel : ViewModel() {

    private val _loginName = MutableLiveData<String?>()
    val loginName: LiveData<String?> = _loginName

    private val _loginPwd = MutableLiveData<String?>()
    val loginPwd: LiveData<String?> = _loginPwd

    private val _loginResult = MutableLiveData<Boolean>()
    val loginResult: LiveData<Boolean> = _loginResult

    fun login(name: String? = null, pwd: String? = null) {
        viewModelScope.launch(Dispatchers.IO) {
            //网络请求
            _loginResult.postValue(true)
        }
    }

    fun bindBundle(bundle: Bundle) {
        val loginName = bundle.getString(LOGIN_NAME)
        val loginPwd = bundle.getString(LOGIN_PASSWORD)
        _loginName.postValue(loginName)
        _loginPwd.postValue(loginPwd)
    }
}